<?php

/**
 * @Description    用户
 * @Author         zhaojiping
 * @Copyright      Copyright (c) 山东艾思软件科技有限公司(https://www.aisisofit.cn)
 * @Date           2022/1/20
 * @CreateBy       VS Code
 */

namespace app\core\controller;

use app\common\controller\CommonController;
use app\core\model\CoreUser;
use app\core\model\CoreRule;
use app\base\model\BaseMenu;
use app\core\model\CoreDepartment;
use app\core\model\CoreTitle;
use think\facade\Db;

class UserController extends CommonController {

    protected $model = null;
    protected $validate = null;

    public function initialize() {
        parent::initialize();
        $this->model = new CoreUser();
        $this->validate = new \app\core\validate\CoreUser();
    }

    /**
     * 获取数据列表
     */
    public function lists() {
        $keyword = input('keyword', '');
        $department_id = input('department_id', 0, 'intval');
        $where = [];
        if ($keyword) $where[] = ['nickname|mobile|username', 'like', "%$keyword%"];
        if ($department_id) // 按组织架构查询
            $where[] = ['department_id', 'in', get_department_ids($department_id)];

        $result = $this->model->getPageList($where);

        json_return(1, '获取成功', $result);
    }

    /**
     * 获取用户拥的的菜单, 并返回树型结构
     * 供web后端菜单显示用
     */
    public function getUserMenu() {
        $where['usable'] = 1;  //获取可用菜单的查询条件
        $where['is_menu'] = 1;  // 是否为菜单, 只查询菜单项
        $coreRule = new CoreRule();
        $result = $coreRule->getUserRoleMenuTree($where);

        json_return(1, '用户拥有的菜单', $result);
    }

    /**
     * 后台重置用户的密码
     */
    public function resetPwd() {
        $id = input('id/d', 0);
        if ($id <= 0) json_return(0, '错误: 参数丢失');

        $user = CoreUser::findOrEmpty($id);
        if ($user->isEmpty()) json_return(0, '用户不存在');

        $user->password = cache('config')['default_password'];
        $user->token = rand_str(18);

        $user->save();
        json_return(1, '密码重复成功, 请使用系统配置密码重新登录');
    }

    /**
     * 修改密码
     */
    public function changePassword() {
        $oldPassword = trim(input('old_password', ''));
        $newPassword = trim(input('new_password', ''));

        $passwordLength =  cache('config')['password_length'];
        if (strlen($newPassword) < $passwordLength)
            json_return(0, '密码需要 ' . $passwordLength . ' 位以上');

        $user = CoreUser::findOrEmpty(UID);
        if ($user->isEmpty()) json_return(0, '用户不存在');

        // 验证用户密码
        if (md5($oldPassword . $user->salt) === $user->password) {
            $user->password = $newPassword;
            $user->save();
            json_return(1, '密码修改成功');
        } else {
            json_return(0, '原密码不正确');
        }
    }

    /**获取基本信息 */
    public function getBaseInfo() {
        $user = CoreUser::field('id,sn,nickname,mobile,avatar,department_id,title_id')->find(UID);
        $department = CoreDepartment::find($user->department_id);
        $title = CoreTitle::find($user->title_id);
        $data = $user->toArray();
        $data = array_merge($data, ['title_name' => @$title->name, 'department_name' => @$department->name]);
        json_return(1, '', $data);
    }

    /**基本信息 */
    public function baseInfo() {
        $user = CoreUser::field('nickname,mobile,avatar')->find(UID);
        if ($this->request->isGet()) {
            json_return(1, '', $user);
        } elseif ($this->request->isPost()) {
            $avatar = input('avatar/s', '');
            $nickname = input('nickname/s', '');
            $mobile = input('mobile/s', '');
            $data = []; // 请求参数处理
            $only = []; // 验证参数
            if ($avatar) {
                $data['avatar'] = $avatar;
                array_push($only, 'avatar');
            }
            if ($nickname) {
                $data['nickname'] = $nickname;
                array_push($only, 'nickname');
            }
            if ($mobile) {
                $data['mobile'] = $mobile;
                if ($user->mobile != $mobile) {
                    array_push($only, 'mobile');
                }
            }
            if (empty($data)) json_return(0, '参数缺失!');
            $this->validate->only($only);
            $res = $this->validate->check($data);
            if (!$res) {
                json_return(0, $this->validate->getError());
            }
            $user->save($data);
            json_return(1, '修改成功');
        }
    }
}
